package org.dallasrowing.logbook.ui.controller;

import org.dallasrowing.logbook.Config;
import org.dallasrowing.logbook.ui.view.AdminWindow;
import org.dallasrowing.logbook.ui.view.BeanEditorWidget;
import org.dallasrowing.logbook.ui.view.LogBookWindow;
import org.dallasrowing.model.Boat;
import org.dallasrowing.model.Rower;
import org.dallasrowing.model.dao.BoatsDAO;
import org.dallasrowing.model.dao.RowersDAO;

public class AdminWindowControllerImpl implements AdminWindowController {

	private LogBookWindow mainWindow;
	private AdminWindow window;
	private RowersDAO rowersDao;
	private BoatsDAO boatsDao;

	public AdminWindowControllerImpl(LogBookWindow mainWindow, RowersDAO rowersDao, BoatsDAO boatsDao)
	{
		this.mainWindow = mainWindow;
		this.rowersDao = rowersDao;
		this.boatsDao = boatsDao;
	}

	public void openWindow() {
		BeanEditorWidget<Rower> widget = window.getRowerEditorWidget();
		new RowerBeanEditorControllerImpl(this, widget, rowersDao);
		BeanEditorWidget<Boat> boatWidget = window.getBoatEditorWidget();
		new BoatEditorControllerImpl(this, boatWidget, boatsDao);
		if (! validatePasswordInput())
		{
			return;
		}
		window.show();
	}


	private boolean validatePasswordInput() {
		String password = window.askPassword();
		return password.equals(Config.adminPassword());
	}

	public void newWindow(AdminWindow window) {
		this.window = window;
	}

	public void close() {
		window.close();
	}

	public void showError(Exception e)
	{
		mainWindow.showError(e);
	}
}
